package com.orangelife.mobile.individual.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.shop.biz.Trader;
import com.orangelife.mobile.widget.OrderPaidMethodPopupWindow.OnPopupWindowItemClickListener;

/**
 * 商家搜索条件的adapter
 * 
 * @author shz
 * 
 */
public class PaidStatusListViewAdapter extends BaseAdapter {

	private Context context;
	private List<String> list;
	private OnPopupWindowItemClickListener mListener;

	public PaidStatusListViewAdapter(Context context, List<String> list) {
		this.context = context;
		this.list = list;
	}

	/** 获得PopupWindowItem的监听器 */
	public void setListener(OnPopupWindowItemClickListener mListener) {

		this.mListener = mListener;
	}

	@Override
	public int getCount() {

		return list.size();
	}

	@Override
	public Object getItem(int position) {

		return list.get(position);
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_shop_condition, null);
			viewHolder = new ViewHolder();
			viewHolder.llPopItem = (LinearLayout) convertView
					.findViewById(R.id.llPopItem);
			viewHolder.tvConditon = (TextView) convertView
					.findViewById(R.id.tvConditon);
			viewHolder.ivIcon = (ImageView) convertView
					.findViewById(R.id.ivIcon);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		/* listview填数据 */

		viewHolder.tvConditon.setText(list.get(position).toString());
		final TextView tvTmp = viewHolder.tvConditon;
		final ImageView ivTmp = viewHolder.ivIcon;
		final String strItemData = list.get(position);
		final int clickPositon = position;
		/* 这里是判断点击了哪个选项，并给相对应的项设置颜色和图片的 */
		int spPosition = Trader.getInstance().getSpTyprPosition();
		if (spPosition == clickPositon) {
			tvTmp.setTextColor(context.getResources().getColor(
					R.color.TextOrange));
			ivTmp.setBackgroundResource(R.drawable.icon_condititon_checked);
		} else {
			tvTmp.setTextColor(context.getResources().getColor(
					R.color.TextBlack));
			ivTmp.setBackgroundResource(R.color.white);
		}
		/* llPopItem的点击监听事件，并将clickPositon, strItemData的值传递出去 */
		viewHolder.llPopItem.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mListener.onPopupWindowItemClick(clickPositon, strItemData);

			}
		});

		return convertView;
	}

	static class ViewHolder {
		/** 搜索条件的值 */
		public TextView tvConditon;
		/** 表示是否选中个图片 */
		public ImageView ivIcon;
		/** 整个item项 */
		public LinearLayout llPopItem;

	}
}
